﻿#include "GraphicsTool/DatasPublic.h"
#include "GraphicsTool/NoLockPool.h"

#include "GraphicsTool/COctreeManager.h"

#include <fstream>
#include <future>

LockPool::LockPool()
{
	mStop = false;
}


void LockPool::Run()
{
	while (!mStop)
	{
		popTask();

		std::unique_lock<std::mutex> loc(mMutex);
		if (mqueue.empty())
		{
			mConditional.wait(loc);
		}
	}
}

LockPool::~LockPool()
{
	shutdown();
}

void LockPool::init(int numThreads)
{
	for (size_t i = 0; i < numThreads; i++)
	{
		mThreads.push_back(std::thread(&LockPool::Run, this));
	}
}

void LockPool::pushTask(std::function<int(int)>& task)
{
	std::unique_lock<std::mutex> loc(mMutex);
	//mqueue.push(std::move(task));


	//改成一次性放进去
}

void LockPool::popTask()
{
	std::unique_lock<std::mutex> loc(mMutex);

	if (!mqueue.empty())
	{
		std::function<int(int)> task = std::move(mqueue.front());
		mqueue.pop();

		task(3);
	}

}

void LockPool::shutdown()
{
	{
		std::unique_lock<std::mutex> loc(mMutex);
		mStop = true;
	}

	mConditional.notify_all();

	for (int i = 0; i < mThreads.size(); ++i)
	{
		if (mThreads.at(i).joinable())
		{
			//运行完，再运行主线程
			mThreads.at(i).join();
		}
	}

}

